package com.key.w8.http1.handler;

import com.key.w8.http1.connection.Connection;
import com.key.w8.http1.connection.ConnectionBuilder;
import com.key.w8.http1.handler.codec.CodecBuilder;
import com.key.w8.http1.handler.codec.ConnectionHttp1Codec;
import com.key.w8.http1.handler.codec.Http1Codec;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;

import java.util.LinkedList;
import java.util.List;

/**
 * @Author: k8
 * @CreateTime: 2025-02-20
 * @Version: 1.0
 */
public class Http1FullHandlerBuilder {
    private boolean isServer;
    private boolean compressResponseContent;
    private long heartbeatInterval;
    private long connectionTimeout;
    private boolean keepAlive;
    /**
     * 负责{@link Http1Codec}的创建，并添加对应的request的编解码
     */
    private CodecBuilder codecBuilder;
    public Http1FullHandlerBuilder(CodecBuilder codecBuilder,boolean keepAlive) {
        assert codecBuilder != null;
        this.codecBuilder = codecBuilder;
        this.keepAlive = keepAlive;
    }

    /**
     * 一般ctx关闭就会关闭整个pipeline以及tcp连接
     * 现在将开端交给connection，其持有ctx，且可以拓展应用层关闭时的其他处理
     */

    private int maxContentLength;
    protected Http1FullHandler build(ConnectionBuilder connectionBuilder){
        if (!isServer) throw new IllegalStateException("Server need connection builder.");
        Http1FullHandler http1FullHandler = null;
        if (keepAlive){
            http1FullHandler = new Http1FullHandler(isServer, heartbeatInterval, connectionTimeout, maxContentLength);
        }else {
            http1FullHandler = new Http1FullHandler(isServer,maxContentLength);
        }
        if (isServer){
            http1FullHandler.setCompressResponseContent(isCompressResponseContent());
        }
        http1FullHandler.setConnectionBuilder(connectionBuilder);
        http1FullHandler.setCodecBuilder(codecBuilder);
        return http1FullHandler;
    }

    public ChannelHandler build(Connection connection) {
        if (isServer) throw new IllegalStateException("Just client need init connection.");
        Http1FullHandler http1FullHandler = new Http1FullHandler(isServer, heartbeatInterval, connectionTimeout, maxContentLength);
        http1FullHandler.setConnection(connection);
        http1FullHandler.setCodecBuilder(codecBuilder);
        return http1FullHandler;
    }



    public boolean isServer() {
        return isServer;
    }

    public Http1FullHandlerBuilder setServer(boolean server) {
        isServer = server;
        return this;
    }

    public boolean isCompressResponseContent() {
        return compressResponseContent;
    }

    public Http1FullHandlerBuilder setCompressResponseContent(boolean compressResponseContent) {
        this.compressResponseContent = compressResponseContent;
        return this;
    }

    public long getHeartbeatInterval() {
        return heartbeatInterval;
    }

    public Http1FullHandlerBuilder setHeartbeatInterval(long heartbeatInterval) {
        this.heartbeatInterval = heartbeatInterval;
        return this;
    }

    public long getConnectionTimeout() {
        return connectionTimeout;
    }

    public Http1FullHandlerBuilder setConnectionTimeout(long connectionTimeout) {
        this.connectionTimeout = connectionTimeout;
        return this;
    }



    public int getMaxContentLength() {
        return maxContentLength;
    }

    public Http1FullHandlerBuilder setMaxContentLength(int maxContentLength) {
        this.maxContentLength = maxContentLength;
        return this;
    }


}
